package com.grass.cropTradingCenter.common;

import lombok.Data;

@Data
public class Result<T>{
    private T data;

    private int code;

    private String msg;

    public Result(){
        this.setCode(ResultCode.SUCCESS.getCode());
        this.setMsg(ResultCode.SUCCESS.getMsg());
    }

    public Result(ResultCode resultCode){
        this.setCode(resultCode.getCode());
        this.setMsg(resultCode.getMsg());
        this.setData(data);
    }

    public Result(ResultCode resultCode, String msg){
        this.setCode(resultCode.getCode());
        this.setMsg(msg);
        this.setData(data);
    }

    public Result(T data){
        this.setCode(ResultCode.SUCCESS.getCode());
        this.setMsg(ResultCode.SUCCESS.getMsg());
        this.setData(data);
    }

    public Result(T data, int code, String msg){
        this.setCode(code);
        this.setMsg(msg);
        this.setData(data);
    }
    public Result(int code, String msg){
        this.setCode(code);
        this.setMsg(msg);
        this.setData(null);
    }

    public static Result error(OutputException outputException) {
        return new Result(outputException.getCode(), outputException.getMessage());
    }

    public static Result error(BusinessException businessException) {
        return new Result(businessException.getCode(), businessException.getMessage());
    }
    /**
     * 方便返回
     */
    public static <T> Result<T> success(T data) {
        return new Result<T>(data);
    }

    public static Result success() {
        return new Result();
    }


}
